﻿// ***********************************************************************
// Assembly         : INNOVACALL.Framework.Common
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="_LayoutViewControllers_Base.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using INNOVACALL.Framework.Common.Extensions;
using INNOVACALL.Framework.Common.MVCHelpers;

namespace INNOVACALL.Framework.Common.Controllers.ViewControllers.LyoutViewControllers
{

    /// <summary>
    /// All View controllers must innherit this base class
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <remarks></remarks>
    [CacheManager(enCacheMode.NoCache)]
    public abstract class _LayoutViewControllers_Base<T> : _ViewControllers_Base where T : INNOVACALL.Framework.Common.Models.ViewModels.LayoutViewModels._LayoutViewModels_Base
    {
        /// <summary>
        /// Gets the URL refer language.
        /// </summary>
        /// <value>The URL refer language.</value>
        /// <remarks></remarks>
        public string URLReferLanguage
        {
            get
            {
                string toReturn = ConfigurationManager.AppSettings["DefaultLanguage"];
                string language = string.Empty;
                string[] tLanguages = ConfigurationManager.AppSettings["Languages"].Split(',');
                if (HttpContext.Request.UrlReferrer != null)
                {
                    string urlReferrer = HttpContext.Request.UrlReferrer.AbsolutePath;
                    if (urlReferrer.Length > 5)
                    {
                        language = urlReferrer.Substring(1, 5).ToLower();
                        if (tLanguages.Contains(language))
                        {
                            toReturn = language;
                        }
                    }
                }
                return toReturn;
            }
        }
        /// <summary>
        /// Add css files (Paht relative to /Content)
        /// </summary>
        protected string[] specificCssCollection = null;
        /// <summary>
        /// Add javascript files (Paht relative to /Scripts) NO JQUERY HERE !
        /// </summary>
        protected string[] specificJSCollection = null;
        /// <summary>
        /// Add links to Javascript generator views
        /// </summary>
        protected string[] dynamicJSCollection = null;
        /// <summary>
        /// Add Javascript JQuery Files here (avoid non JQuery)
        /// </summary>
        protected string[] specificJqueryJSCollection = null;
        /// <summary>
        /// If you need to use external JS files
        /// </summary>
        protected string[] externalJs = null;
        /// <summary>
        /// If you need to use external css files
        /// </summary>
        protected string[] externalCss = null;
        /// <summary>
        /// If you need to initialize some javascript variables already available for the $.docReady handler
        /// Use javascript declaration like [var doSomething=true] or [var myMessage='hello it\'s me']
        /// </summary>
        protected string[] jsVariables = null;

        /// <summary>
        /// The meta
        /// </summary>
        protected Dictionary<enMeta, string> Meta = new Dictionary<enMeta, string>();

        #region  Variables membres
        /// <summary>
        /// The _content data
        /// </summary>
        private T _contentData;
        #endregion

        /// <summary>
        /// The lien config
        /// </summary>
        private string lienConfig = "";
        /// <summary>
        /// Gets or sets the content data.
        /// </summary>
        /// <value>The content data.</value>
        /// <remarks></remarks>
        public T ContentData
        {
            get
            {
                return _contentData;
            }
            set
            {
                _contentData = value;
            }
        }

        /// <summary>
        /// Gets the name of the theme.
        /// </summary>
        /// <value>The name of the theme.</value>
        /// <remarks></remarks>
        public string ThemeName
        {
            get
            {
                if (Session["ThemeName"] != null)
                {
                    return Session["ThemeName"].ToString();
                }
                else
                {
                    return ConfigurationManager.AppSettings["DefaultThemeName"].ToString();
                }
            }
        }



        /// <summary>
        /// Initializes a new instance of the <see cref="_LayoutViewControllers_Base{T}" /> class.
        /// </summary>
        /// <remarks></remarks>
        public _LayoutViewControllers_Base()
        {
            ContentData = Activator.CreateInstance<T>();
        }

        /// <summary>
        /// Crée un objet résultat de contenu à l'aide d'une chaîne, du type de contenu et de l'encodage du contenu.
        /// </summary>
        /// <param name="content">Contenu à écrire dans la réponse.</param>
        /// <param name="contentType">Type de contenu (type MIME).</param>
        /// <param name="contentEncoding">Encodage du contenu.</param>
        /// <returns>Instance de résultat de contenu.</returns>
        /// <remarks></remarks>
        protected override ContentResult Content(string content, string contentType, System.Text.Encoding contentEncoding)
        {
            return base.Content(content.Replace("\t", string.Empty), contentType, contentEncoding);
        }
        /// <summary>
        /// Initilializations the view (Framework Step 2).
        /// </summary>
        /// <param name="pageTitle">The page title.</param>
        /// <param name="pageMessage">The page message.</param>
        /// <param name="specificCssCollection">The specific CSS collection.</param>
        /// <param name="specificJSCollection">The specific JS collection.</param>
        /// <param name="dynamicJSCollection">The dynamic JS collection.</param>
        /// <param name="specificJqueryJSCollection">The specific jquery JS collection.</param>
        /// <param name="jsVariables">The js variables.</param>
        /// <param name="externalCss">The external CSS.</param>
        /// <param name="displayMenus">if set to <c>true</c> [display menus].</param>
        /// <param name="metaToAdd">The meta to add.</param>
        /// <param name="usesKendoUI">if set to <c>true</c> [uses kendo UI].</param>
        /// <param name="jQueryUITheme">The j query UI theme.</param>
        /// <param name="kendoUITheme">The kendo UI theme.</param>
        /// <remarks></remarks>
        [CacheManager(enCacheMode.NoCache)]
        protected void InitView_Framework(string pageTitle, string pageMessage, string[] specificCssCollection, string[] specificJSCollection, string[] dynamicJSCollection, string[] specificJqueryJSCollection, string[] jsVariables, string[] externalJS, string[] externalCss, bool displayMenus, Dictionary<enMeta, string> metaToAdd, bool usesKendoUI, string jQueryUITheme, string kendoUITheme)
        {
            INNOVACALL.Framework.Common.Models.ViewModels.LayoutViewModels._LayoutViewModels_Base contentData = ContentData as INNOVACALL.Framework.Common.Models.ViewModels.LayoutViewModels._LayoutViewModels_Base;
            contentData._DisplayMenus = displayMenus;
            contentData._UserLogin = "#Anonyme#";
            contentData._PageTitle = pageTitle;
            contentData._PageMessage = pageMessage;
            contentData._UsesKendoUI = usesKendoUI;
            contentData._JQueryUITheme = jQueryUITheme;
            contentData._KendoUITheme = kendoUITheme;
            contentData._Meta.Add(enMeta.Author, "Laurent Jordi");
            contentData._Meta.Add(enMeta.Copyright, "Innovacall 2012");
            contentData._Meta.Add(enMeta.Generator, "Innovacall Open Source ASP.net MVC 4 Framework");
            contentData._Meta.Add(enMeta.Rating, "General");
            contentData._Meta.Add(enMeta.Rev, "framework/'@'/innovacall.com");
            contentData._Meta.Add(enMeta.Robots, "index, follow");
            foreach (var item in metaToAdd)
            {
                contentData._Meta.Add(item.Key, item.Value);
            }
            if (User != null && User.Identity != null && User.Identity.Name.Length > 0) contentData._UserLogin = User.Identity.Name;
            if (specificCssCollection != null)
            {
                contentData._SpecificCSS.AddRange(specificCssCollection.ToList());
            }
            if (specificJSCollection != null)
            {
                contentData._SpecificJS.AddRange(specificJSCollection.ToList());
            }

            if (specificJqueryJSCollection != null)
            {
                contentData._SpecificJqueryJS = specificJqueryJSCollection.ToList();
            }

            if (dynamicJSCollection != null)
            {
                contentData._DynamicJqueryJS = dynamicJSCollection.ToList();
            }
            if (externalJs != null)
            {
                contentData._externalJs = externalJs.ToList();
            }
            if (externalCss != null)
            {
                contentData._externalJs = externalCss.ToList();
            }
            if (jsVariables != null)
            {
                contentData._JSVariables = jsVariables.ToList();
            }
        }

        /// <summary>
        /// Reads the file JS stream.
        /// </summary>
        /// <param name="UrlSpecificJqueryJSFile">The URL specific jquery JS file.</param>
        /// <returns>System.String.</returns>
        /// <remarks></remarks>
        public string ReadFileJSStream(string UrlSpecificJqueryJSFile)
        {
            string ligne = null;
            string pathfile = HttpContext.Server.MapPath(UrlSpecificJqueryJSFile);

            var sr = new StreamReader(pathfile);
            ligne = sr.ReadToEnd();
            sr.Close();
            return ligne;
        }
    }
}
